﻿using System;
using System.Runtime.InteropServices;

namespace Eflong.Structure
{
    public enum VCS_MT_TYPES
    {
        VCS_MT_DEFAULT , VCS_MT_LOGIN , VCS_MT_LOGOUT ,
        VCS_MT_UPDATEDEFINITION , VCS_MT_UPDATEDATA ,
        VCS_MT_QUERYDEFINITION , VCS_MT_QUERYDATA , VCS_MT_QUERYRECORD ,
        VCS_MT_QUERYREPORT , VCS_MT_SAVEOPTIONS , VCS_MT_QUERYCOALMINEINFO ,
        VCS_MT_QUERYREPORTTEMPLET , VCS_MT_QUERYREPORTTEMPLETDATA
    };

    [StructLayout( LayoutKind.Sequential )]
    public struct VizClientServer
    {
        public const int VCS_MT_LOGIN = 1;
        public const int VCS_MT_LOGOUT = 2;
        public const int VCS_MT_UPDATEDEFINITION = 3;
        public const int VCS_MT_UPDATEDATA = 4;
        public const int VCS_MT_QUERYDEFINITION = 5;
        public const int VCS_MT_QUERYDATA = 6;
        public const int VCS_MT_QUERYRECORD = 7;

        public const int VCS_MT_QUERYREPORT = 8;
        public const int VCS_MT_SAVEOPTIONS = 9;
        public const int VCS_MT_QUERYCOALMINEINFO = 10;
        public const int VCS_MT_QUERYREPORTTEMPLET = 11;
        public const int VCS_MT_QUERYREPORTTEMPLETDATA = 12;

        public const int VCS_MT_LOGIN_EX = 31;
        public const int VCS_MT_LOGOUT_EX = 32;
        public const int VCS_MT_UPDATEDEFINITION_EX = 33;
        public const int VCS_MT_UPDATEDATA_EX = 34;
        public const int VCS_MT_QUERYDEFINITION_EX = 35;
        public const int VCS_MT_QUERYDATA_EX = 36;
        public const int VCS_MT_QUERYRECORD_EX = 37;

        public const int VCS_MT_UPDATEDEFINITION_NORMAL = 43;

        public const int VCS_NT_SENDDEFINITION = 1;
        public const int VCS_NT_SENDDATA = 2;
        public const int VCS_NT_UPDATEDEFINITION = 3;
        public const int VCS_NT_UPDATEDATA = 4;
        public const int VCS_NT_CONNECTIONSTATUSCHANGED = 5;

        public const int VCS_NT_SENDDEFINITION_EX = 6;
        public const int VCS_NT_SENDDATA_EX = 7;
        public const int VCS_NT_UPDATEDEFINITION_EX = 8;
        public const int VCS_NT_UPDATEDATA_EX = 9;
        public const int VCS_NT_CONNECTIONSTATUSCHANGED_EX = 10;

        public const int VCS_MR_OK = 1;
        public const int VCS_MR_UNKNOWNMETHOD = 0;
        public const int VCS_MR_FAILURE = ( -1 );
        public const int VCS_MR_FAILTOCONNECT = ( -11 );
        public const int VCS_MR_FAILTODISCONNECT = ( -12 );
        public const int VCS_MR_FAILTOREQUEST = ( -21 );
        public const int VCS_MR_FAILTOACK = ( -22 );
        public const int VCS_MR_FAILTOLOGIN = ( -23 );
        public const int VCS_MR_FAILTOLOGOUT = ( -24 );

        public uint m_dwMethodType;
        public int m_nMethodAck;

        public int Size( )
        {
            return Marshal.SizeOf( this );
        }

        public byte[ ] GetByteArray( )
        {
            byte[ ] ba = new byte[ 8 ];
            IntPtr ptr = Marshal.UnsafeAddrOfPinnedArrayElement( ba , 0 );
            Marshal.StructureToPtr( this , ptr , true );
            return ba;
        }

        public static VizClientServer ConverFromByteArray( byte[ ] ba )
        {
            //GCHandle pinnedPacket = GCHandle.Alloc(ba, GCHandleType.Pinned);
            //// IntPtr ptr = pinnedPacket.AddrOfPinnedObject();

            //VizClientServer vcs = (VizClientServer)Marshal.PtrToStructure(
            //    pinnedPacket.AddrOfPinnedObject(),
            //    typeof(VizClientServer));
            //pinnedPacket.Free();
            //return vcs;
            return StructConvert.PtrToStruct<VizClientServer>( ba );
        }
    }
}